package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.bean.Student;
import org.example.feign.SchoolFeign;
import org.example.feign.StudentFeign;
import org.example.mapper.StudentMapper;
import org.example.service.StudentService;
import org.example.util.JsonResult;
import org.example.util.ResultTool;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther:Liu
 * @Testname:StudentServiceImpl
 * @Date:2025/9/9 13:34
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private StudentFeign studentFeign;
    @Resource
    private SchoolFeign schoolFeign;

    @Override
    public JsonResult SaveStudent(Student student) {
        save(student);
        return ResultTool.success("添加成功");
    }

    @Cacheable(cacheNames = "findStudentById")
    public JsonResult findStudentById(String id) {
        Student student = studentMapper.selectById(id);
        JsonResult course = studentFeign.findCourseById(student.getCourseId());
        student.setCourse(course);
        JsonResult school = schoolFeign.findById(student.getSchoolId());
        student.setSchool(school);
        return ResultTool.success(student);
    }

    @Cacheable(cacheNames = "findStudentByNo")
    public JsonResult findStudentByNo(String studentNo) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_no", studentNo);
        Student student = studentMapper.selectOne(wrapper);
        JsonResult course = studentFeign.findCourseById(student.getCourseId());
        student.setCourse(course);
        JsonResult school = schoolFeign.findById(student.getSchoolId());
        student.setSchool(school);
        return ResultTool.success(student);
    }
}
